package cn.qh.seasonal.controller;

import cn.qh.seasonal.biz.UserBiz;
import cn.qh.seasonal.exception.NotFoundException;
import cn.qh.seasonal.util.JwtUtil;
import cn.qh.seasonal.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qh.seasonal.entity.User;
import cn.qh.seasonal.dto.UserDTO;
import cn.qh.seasonal.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;

/**
 * 控制层
 * 控制层
 **/
@RestController
@RequestMapping("user")
@Api
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private UserBiz userBiz;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int")
    @CrossOrigin
    public Page<User> selectAll(@RequestParam(defaultValue = "1") int current) {
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(3);
        this.userService.page(page);
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public User selectOne(@PathVariable Serializable id) {
        return this.userService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param userDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = CommonResponse.class)
    @CrossOrigin
    public boolean insert(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        return this.userService.save(user);
    }

    /**
     * 修改数据
     *
     * @param userDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "PUT", response = CommonResponse.class)
    @CrossOrigin
    public boolean update(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        return this.userService.updateById(user);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Serializable id) {
        return this.userService.removeById(id);
    }


    @PostMapping("/doLogin")
    public CommonResponse doLogin(@Validated @RequestBody UserDTO userDTO) {
        User login = userBiz.login(userDTO.getUserName(), userDTO.getUserPassWord());
        if (login == null) {
            throw new NotFoundException(1003);
        }
        String sign = JwtUtil.sign(userDTO.getUserId(), userDTO.getUserName());
        if (!JwtUtil.verity(sign)) {
            throw new NotFoundException(1003);
        }

        return CommonResponse.builder().code(200).message("11").data(sign).build();
    }

    @GetMapping("/yi")
    public List<User> yi(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null || token.trim().equals("")) {
            throw new NotFoundException(1003);
        }
        if (!JwtUtil.verity(token)) {
            throw new NotFoundException(1003);
        }
        return userService.list();
    }
}
